package com.serkol.abmanager.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.serkol.abmanager.bean.GridResult;
import com.serkol.abmanager.bean.Result;
import com.serkol.abmanager.constant.ResultConstant;
import com.serkol.abmanager.entity.Automobiles;
import com.serkol.abmanager.entity.Goods;
import com.serkol.abmanager.entity.Users;
import com.serkol.abmanager.service.GoodsService;
import com.serkol.abmanager.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@RequestMapping("api/users")
@Api(tags = "人员表")
@ResponseBody
public class UsersApiController {

    @Autowired
    private UsersService service;

    @ApiOperation(value = "分页")
    @PostMapping("/findByList")
    public GridResult findByList(@RequestParam(name = "name", defaultValue = "") String name, @RequestParam(name = "groupId", defaultValue = "0") long groupId, @RequestParam(name = "rfid", defaultValue = "0") long rfid, @RequestParam(name = "deptId", defaultValue = "0") long deptId, @RequestParam(name = "roleId", defaultValue = "0") int roleId, @RequestParam(name = "groupType", defaultValue = "0") int groupType, @RequestParam(name = "page", defaultValue = "0") int page, @RequestParam(name = "pagesize", defaultValue = "10") int pagesize) {

        GridResult page1 = service.findByList(page, pagesize, name, rfid, deptId, groupId, groupType, roleId);

        return page1;
    }

    @ApiOperation(value = "查询")
    @PostMapping("/findById")
    public Result findById(@RequestParam(name = "id", defaultValue = "0") long id) {
        if (id > 0) {
            Users byId = service.getById(id);
            if (byId != null) {
                return Result.ok(byId);
            } else {
                return Result.failure(ResultConstant.EMPTIY);
            }
        } else {
            return Result.failure();
        }


    }

    @ApiOperation(value = "删除")
    @PostMapping("/findByDelete")
    public Result findByDelete(@RequestParam(name = "id", defaultValue = "0") long id) {
        if (id > 0) {
            boolean b = service.removeById(id);
            if (b) {
                return Result.ok();
            } else {
                return Result.failure(ResultConstant.EMPTIY);
            }
        } else {
            return Result.failure();
        }


    }

    @ApiOperation(value = "添加或编辑")
    @PostMapping("/edit")
    public Result edit(@RequestParam(name = "id", defaultValue = "0") long id,
                       @RequestParam(name = "name", defaultValue = "") String name,
                       @RequestParam(name = "rfid", defaultValue = "") String rfid,
                       @RequestParam(name = "tell", defaultValue = "") String tell,
                       @RequestParam(name = "email", defaultValue = "") String email,
                       @RequestParam(name = "groupType", defaultValue = "0") int groupType,
                       @RequestParam(name = "groupId", defaultValue = "0") long groupId,
                       @RequestParam(name = "deptId", defaultValue = "0") long deptId,
                       @RequestParam(name = "carId",defaultValue = "0") long carId,
                       @RequestParam(name = "roleId", defaultValue = "0") int roleId,
                       @RequestParam(name = "user_name", defaultValue = "") String user_name,
                       @RequestParam(name = "user_pass", defaultValue = "") String user_pass) {
        Result result = new Result();
        if (!user_name.isEmpty() && !name.isEmpty() && !tell.isEmpty() && !email.isEmpty()) {

            Users users = new Users();
            users.setDeptId(deptId);
            users.setEmail(email);
            users.setTell(tell);
            users.setName(name);
            users.setRfid(rfid);
            users.setRoleId(roleId);
            users.setCarId(carId);
            users.setGroupType(groupType);
            users.setGroupId(groupId);
            users.setUserName(user_name.toLowerCase());
            if (!user_pass.isEmpty()) {
                String encode = DigestUtils.md5Hex(user_pass);
                users.setUserPass(encode);
            }
            boolean flag = false;
            if (id > 0) {
                users.setId(id);
                flag = service.updateById(users);
                users = service.getById(users.getId());
            } else {
                QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(Users::getUserName, user_name);
                if (service.count(queryWrapper) == 0) {
                    flag = service.save(users);
               /*     queryWrapper.lambda().eq(Users::getName, name);
                    queryWrapper.lambda().eq(Users::getDeptId, deptId);
                    queryWrapper.lambda().eq(Users::getEmail, email);
                    users = service.getOne(queryWrapper);*/


                } else {
                    result.setMsg("用户名已注册");
                }
            }
            if (flag) {
                result.setData(users);
                result.setStatus(ResultConstant.SUCSESS);
            } else {
                result.setStatus(ResultConstant.FAIL);
            }
        } else {
            result.setMsg("不能为空");
        }

        return result;

    }

    @ApiOperation(value = "登录信息添加或编辑")
    @PostMapping("/editLogin")
    public Result editLogin(@RequestParam(name = "id", defaultValue = "0") long id,
                            @RequestParam(name = "user_name", defaultValue = "") String user_name,
                            @RequestParam(name = "user_pass", defaultValue = "") String user_pass
    ) {
        Result result = new Result();
        if (!user_name.isEmpty() && !user_pass.isEmpty()) {
            Users users = new Users();
            String encode = DigestUtils.md5Hex(user_pass);
            users.setUserName(user_name);
            users.setUserPass(encode);

            boolean flag = false;
            if (id > 0) {
                users.setId(id);
                flag = service.updateById(users);
            } else {
                QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(Users::getUserName, user_name);
                if (service.count(queryWrapper) == 0) {
                    flag = service.save(users);
                } else {
                    result.setMsg("已注册");
                }

            }
            if (flag) {
                result.setStatus(ResultConstant.SUCSESS);
            } else {
                result.setStatus(ResultConstant.FAIL);
            }
        } else {
            result.setMsg("不能为空");
        }

        return result;

    }
}
